import Taro from '@tarojs/taro'
// 设置标题
const setTitleInfo = (title = '') => {
    if (window.bridge) {
        window.bridge.callHandler('getWebTitle', { title }, function responseCallback(res) { })
    }
}
// 打电话
const setTelphoneInfo = (tephone = '') => {
    if (window.bridge) {
        window.bridge.callHandler('call', { tephone }, function responseCallback(res) { })
    }
}
// 私聊
const setWechatInfo = (tephone = '', type = '0', name = '') => {
    // type 为提示语 1:寻车 2:车源
    // name 名称
    function getName(type) {
        return {
            $0: '',
            $1: '寻车',
            $2: '车源',
            $3: '物流',
            $4: '报价信息'
        }[`$${type}`]
    }
    const tips = `您好！我对您发布的${getName(type)}[${+type === 3 ? name.split('/')[0] : name}] ${+type === 3 ? '[' + name.split('/')[1] + '--' + name.split('/')[2] + ']' : ''}有兴趣。`
    if (window.bridge) {
        window.bridge.callHandler('IMChat', { tephone, tips }, function responseCallback(res) { })
    }
}
// 跳转原生页面
const setSkipCustom = (type = '') => {
    if (window.bridge) {
        // type (0首页 1寻车列表 2运输 3消息 4我的 5车源列表)
        window.bridge.callHandler('skipCustom', { "type": type + "" }, function responseCallback(res) { })
    } else {
        Taro.navigateBack({
            delta: 1
        })
    }
}

// 设备判断方法
const judgeClient = () => {
    let u = window.navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;   //判断是否是 android终端
    let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);     //判断是否是
    if (isAndroid) {
        return 'Android';
    } else if (isIOS) {
        return 'IOS';
    } else {
        return 'PC';
    }
}

const SDK = {
    setTitleInfo,
    setTelphoneInfo,
    setWechatInfo,
    setSkipCustom,
    judgeClient
}

export default SDK
